import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Spinner extends JApplet {  

	private MyCanvas canvas;
	private JMenuBar Menubar;
	private JMenu Threadstate;
	private JMenuItem Start, Stop;

	public Spinner ( )   {
		canvas = new MyCanvas();
		getContentPane().add ( canvas, BorderLayout.CENTER );

        	Menubar = new JMenuBar ( );
      		setJMenuBar(Menubar);

           	Threadstate = new JMenu ( "GO FOR" );

            	Start = new JMenuItem ( "A SPIN" );
            	Threadstate.add ( Start );
            	Start.addActionListener ( canvas );

            	Menubar.add ( Threadstate );
	}

   	class MyCanvas extends JPanel implements ActionListener, Runnable   {
		private Thread runner;
		private int spinnum, startplace;
		private boolean running;

		public MyCanvas ( )   {		//  Initialize the variable.
			setBackground ( Color.black );
			startplace = spinnum = 0;
			running = false;
		}

		public void paintComponent (Graphics g) {
			super.paintComponent ( g );
			g.setColor ( Color.white );
			g.fillOval ( 15, 15, 470, 470 );
			g.setColor ( Color.red );
			g.fillArc ( 15, 15, 470, 470, 75, 30 );
			for ( int i = 0; i < 12; i++ )
			{
				g.setColor ( Color.black );
				g.fillArc ( 20, 20, 460, 460, 17 + i * 30 - startplace * 5, 26 );
			}

			Font MyFont = new Font ( "Serif", Font.BOLD, 42 );
			g.setFont ( MyFont );

			for ( int i = 1; i <= 12; i++ )
			{
				g.setColor ( Color.white );
				if ( (startplace + i * 6 + 3) % 72 >= 0 && (startplace + i * 6 + 3) % 72 < 6 )
				{
					System.out.println ( startplace + "   " + i );
					g.setColor ( Color.red );
				}
				g.drawString ( "" + i, 235 + (int)(190 * Math.cos(Math.PI / 180 * ((i - 3) * 30 + startplace * 5))), 
						    265 + (int)(190 * Math.sin(Math.PI / 180 * ((i - 3) * 30 + startplace * 5))) );
			}
		}

		void startRunning() {
			if (running)
				return;
			runner = new Thread(this);
			running = true;
			spinnum = 72 + 6 * (int)(24 * Math.random());
			runner.start();
		}

//		void stopRunning() {
//			running = false;
//		}

		public void run() {
			for ( int i = 0; i < spinnum; i++ )   {
				startplace++;
				if (startplace == 72)
					startplace = 0;
				repaint ( );
				runner.yield();
				Pause ( 30 );
//				Pause ( ( 6 * i + 180 - spinnum) / 4 );
			}
			running = false;
		}

		public void Pause ( int milli )   {
			try   {
				Thread.sleep ( milli );
			}
			catch ( InterruptedException e )   { }
		}

		public void actionPerformed ( ActionEvent e )   {
			if ( e.getSource ( ) == Start )
				startRunning ( );
      		}
	}
}
